package com.hspedu.tankgame03;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Vector;

public class MyPanel extends JPanel implements KeyListener,Runnable {
    //定义我的坦克
    HeroTank hero1 = null;

    //定义敌人坦克，多线程用Vector
    Vector<EnemyTank> enemyTanks = new Vector<>();
    int enemyTankeSize = 3;

    public MyPanel() {
        //初始化我的坦克
        hero1 = new HeroTank(100, 100);
        //初始化敌人坦克
        for (int i = 0; i < enemyTankeSize; i++) {
            enemyTanks.add(new EnemyTank(100*(i+1), 0));
        }
    }

    @Override
    public void paint(Graphics g){
        super.paint(g);
        g.fillRect(0,0,1000,750);
        //画自己的坦克
        drawTank(hero1.getX(), hero1.getY(), g, hero1.getDirect(),1);
        //画敌人的坦克
        for (int i = 0; i < enemyTanks.size(); i++) {
            EnemyTank enemyTank = enemyTanks.get(i);
            drawTank(enemyTank.getX(), enemyTank.getY(), g, 2, 0);
        }
        //画hero设计的子弹；
        if (hero1.shot != null && hero1.shot.isLive == true){
            drawShot(hero1.shot.x, hero1.shot.y, g, 1); //画子弹
        }
    }

    /**
     * @param x 坦克的左上角x坐标
     * @param y 坦克的左上角y坐标
     * @param g 画笔
     * @param direct 坦克的方向
     * @param type 坦克类型
     * */
    public void drawTank(int x, int y, Graphics g, int direct, int type){
        switch (type){
            case 0: //我们的坦克
                g.setColor(Color.cyan);
                break;
            case 1:
                g.setColor(Color.yellow);
                break;
        }
        //根据坦克方向来绘制坦克
        switch (direct){
            case 0: //向上
                g.fill3DRect(x, y, 10, 60, false);
                g.fill3DRect(x+30, y, 10, 60, false);
                g.fill3DRect(x+10, y+10, 20, 40, false);
                g.fillOval(x+10, y+20, 20, 20);
                g.drawLine(x+20, y+30, x+20, y);
                break;
            case 1: //向右
                g.fill3DRect(x, y, 60, 10, false);
                g.fill3DRect(x, y+30, 60, 10, false);
                g.fill3DRect(x+10, y+10, 40, 20, false);
                g.fillOval(x+20, y+10, 20, 20);
                g.drawLine(x+30, y+20, x+60, y+20);
                break;
            case 2: //向下
                g.fill3DRect(x, y, 10, 60, false);
                g.fill3DRect(x+30, y, 10, 60, false);
                g.fill3DRect(x+10, y+10, 20, 40, false);
                g.fillOval(x+10, y+20, 20, 20);
                g.drawLine(x+20, y+30, x+20, y+60);
                break;
            case 3: //向左
                g.fill3DRect(x, y, 60, 10, false);
                g.fill3DRect(x, y+30, 60, 10, false);
                g.fill3DRect(x+10, y+10, 40, 20, false);
                g.fillOval(x+20, y+10, 20, 20);
                g.drawLine(x+30, y+20, x, y+20);
                break;
            default:
                System.out.println("暂时没有处理");
        }
    }

    public void drawShot(int x, int y, Graphics g, int type){
        switch (type){
            case 0: //我们的坦克
                g.setColor(Color.cyan);
                break;
            case 1:
                g.setColor(Color.yellow);
                break;
        }
        g.fill3DRect(x, y, 5, 5, false);
    }

    @Override
    public void keyTyped(KeyEvent e) {

    }

    @Override
    public void keyPressed(KeyEvent e) {
        //移动
        if(e.getKeyCode() == KeyEvent.VK_W){
            hero1.setDirect(0);
            hero1.moveUp();
        }else if(e.getKeyCode() == KeyEvent.VK_D){
            hero1.setDirect(1);
            hero1.moveRight();
        }else if (e.getKeyCode() == KeyEvent.VK_S){
            hero1.setDirect(2);
            hero1.moveDown();
        }else if (e.getKeyCode() == KeyEvent.VK_A){
            hero1.setDirect(3);
            hero1.moveLeft();
        }


        //射击
        if (e.getKeyCode() == KeyEvent.VK_J){
            hero1.shotEnemyTank();
        }

        this.repaint();
    }

    @Override
    public void keyReleased(KeyEvent e) {

    }

    @Override
    public void run() {  //每隔100毫秒，重绘区域
        while (true){
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            this.repaint();
        }
    }
}
